package com.baizhi.shane.shane.service.impl;

import com.baizhi.shane.constant.SysMapping;
import com.baizhi.shane.shane.vo.EssayUserRelVO;
import com.baizhi.shane.sys.manager.RedisManager;
import com.baizhi.shane.sys.utils.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baizhi.shane.shane.entity.ShaneAgainstEssay;
import com.baizhi.shane.shane.service.ShaneAgainstEssayService;
import com.baizhi.shane.shane.mapper.ShaneAgainstEssayMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author wbz12
 * @description 针对表【shane_against_essay(啥呢文章反对表)】的数据库操作Service实现
 * @createDate 2023-07-25 22:33:37
 */
@Service
public class ShaneAgainstEssayServiceImpl extends ServiceImpl<ShaneAgainstEssayMapper, ShaneAgainstEssay>
        implements ShaneAgainstEssayService {

    @Resource
    private RedisManager redisManager;
    @Resource
    private CommonUtils commonUtils;

    /**
     * 用户反对文章，只需要answerId即可，userId自动分配为当前用户
     *
     * @return java.lang.String
     * @Author 闻柏智
     * @Date 21:43 2023/10/24
     * @Param essayUserRelVO
     **/
    @Override
    public String insertOneEssayAgainst(EssayUserRelVO essayUserRelVO) {
        Long userId = redisManager.getUserId();
        Long essayId = essayUserRelVO.getEssayId();
        ShaneAgainstEssay shaneAgainstEssay = new ShaneAgainstEssay();
        shaneAgainstEssay.setUserId(userId);
        shaneAgainstEssay.setEssayId(essayId);
        boolean save = this.save(shaneAgainstEssay);
        String succeed = commonUtils.boolToString(save, SysMapping.AGAINST_SUCCESS, SysMapping.AGAINST_FAIL);
        return succeed;
    }

    /**
     * 用户取消反对文章，只需要answerId即可，userId自动分配为当前用户
     *
     * @Author 闻柏智
     * @Date 21:49 2023/10/24
     * @Param essayUserRelVO
     * @return java.lang.String
     **/
    @Override
    public String deleteOneEssayAgainst(EssayUserRelVO essayUserRelVO) {
        Long userId = redisManager.getUserId();
        Long essayId = essayUserRelVO.getEssayId();

        LambdaUpdateWrapper<ShaneAgainstEssay> uwSae = new LambdaUpdateWrapper<>();
        uwSae.eq(ShaneAgainstEssay::getUserId, userId);
        uwSae.eq(ShaneAgainstEssay::getEssayId, essayId);
        uwSae.eq(ShaneAgainstEssay::getDelFlag, 0);
        uwSae.set(ShaneAgainstEssay::getDelFlag, 1);
        uwSae.set(ShaneAgainstEssay::getDelBy, userId);
        uwSae.set(ShaneAgainstEssay::getDelTime, new Date());
        boolean update = this.update(uwSae);
        String succeed = commonUtils.boolToString(update, SysMapping.AGAINST_CANCEL_SUCCESS, SysMapping.AGAINST_CANCEL_FAIL);
        return succeed;
    }
}




